﻿using DataAccess;
using KhoaLuan.Utils;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.ProcessAJAX
{
    public partial class AjaxMenuWeb : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string GetListMenuWeb(int currentpage, int numberinpage, string keyword)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementMenuWeb.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.View))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xem danh sách các menu web" });
            try
            {
                MenuWeb_Proxy pr = new MenuWeb_Proxy();
                List<DataAccess.MenuWeb> list = new List<DataAccess.MenuWeb>();
                int totalitem = 0;
                if (keyword == "null" || keyword == "")
                {
                    list = pr.SelectAllMenuWeb_Proxy();
                    //xuat all
                }
                else
                {
                    list = (pr.SelectNo_MenuWeb_Proxy(keyword)).ToList<DataAccess.MenuWeb>();
                }
                totalitem = list.Count;
                var list2 = list.Skip((currentpage - 1) * numberinpage).Take(numberinpage);
               
                return new JavaScriptSerializer().Serialize(new { Status = "Success", Data = list2, TotalItem = totalitem, CurrentPage = currentpage });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error", Data = "" });
            }
        }
        [WebMethod]
        public static string DeleteMenuWeb(int id)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementMenuWeb.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Delete))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xóa menu web" });
            try
            {
                MenuWeb_Proxy pr = new MenuWeb_Proxy();
                if (pr.Delete_MenuWeb_Proxy(id))
                    return new JavaScriptSerializer().Serialize(new { Status = "Success" });
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }
        [WebMethod]
        public static string InserttbMenuWeb(string Name, int ParentID, string Link, int OrgID)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementMenuWeb.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.AddNew))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền thêm menu web" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbIntroduce).Name);
                MenuWeb_Proxy pr = new MenuWeb_Proxy();
                DataAccess.MenuWeb a = new DataAccess.MenuWeb();
                a.id = 0;
                a.Name = Name;
                a.ParentID = ParentID;
                a.Link = Link;
                a.OrgID = OrgID;
                if (pr.Insert_MenuWeb_Proxy(a))
                    log.Info(UserLogin.GetUserName + "Thêm mới Menu Web thành công");
                return new JavaScriptSerializer().Serialize(new { Status = "Success" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }
        [WebMethod]
        public static string UpdatetbMenuWeb(int id, string Name, int ParentID, string Link, int OrgID)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementMenuWeb.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Edit))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền chỉnh sửa menu web" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbIntroduce).Name);
                MenuWeb_Proxy pr = new MenuWeb_Proxy();
                DataAccess.MenuWeb a = new DataAccess.MenuWeb();
                a.id = id;
                a.Name = Name;
                a.ParentID = ParentID;
                a.Link = Link;
                a.OrgID = OrgID;
                if (pr.Update_MenuWeb_Proxy(a))
                    log.Info(UserLogin.GetUserName + "Cập nhật MenuWeb thành công");
                return new JavaScriptSerializer().Serialize(new { Status = "Success" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }
    }
}